while¶

Consider the following task: fill the bottom row of a bit world with green.

No description has been provided for this image

No description has been provided for this image

🎨 while¶

ever_green.py¶

Change True to False - what happens?

while <condition>:
    if the condition was True
    run these lines
    then check the condition again

Repeating the same lines of code is called a loop.

A loop body contains the instructions we want to run multiple times.

The loop condition determines whether the loop body will be run.

The loop body is indented 4 spaces relative to the while statement.

Ideally, we would have something that was True when it was ok for Bit to move and False when Bit was blocked.

Helpful Bit Fact #1¶

Bit has a method for checking whether the front of Bit is clear (i.e. a move will not go out of bounds).

bit.can_move_front()

It returns True when the space in front of Bit is clear and False when the space in front is blocked.

go_green.py¶

NOTES

Step through the code, one step at a time.

Point out how the loop condition is checked before each run of the body.

Show how the loop body is skipped when the condition is False.

If necessary, change the last line to bit.paint('blue') to clarify that the body is completely skipped when the condition is false.

Follow a similar approach for each of the demonstrations in this lesson.

green_blue.py¶

Conditions¶

A condition is something that is either true or false.

We can represent the concept of true or false in python using the values True and False.

A thing that is either True or False is called a boolean (named after George Boole).

Boundary Conditions¶

paint_move.py¶

In paint_move.py, why isn't the last square painted?

Change the order of the paint and move statements. What happens?

When using while loops, always consider the boundary conditions.

A boundary condition is the state of your program when a loop starts or finishes.

For example:

  • Where is Bit when the loop starts?
  • Where will Bit be when the loop ends?
  • What squares will be colored? Which squares will not be colored?
  • Which direction is Bit facing at the beginning? at the end?
When writing a while loop, be clear about the boundary conditions.

Helpful Bit Fact #2¶

Sometimes a Bit world includes black squares. These squares are blocked; Bit cannot move onto them.

No description has been provided for this image

Besides can_move_front(), Bit can also check can_move_left() and can_move_right().

black_row.py¶

How can we move the bit to be next to the first black square?

No description has been provided for this image No description has been provided for this image

🎨 not¶

You can turn a True to False and a False to True using the not keyword.

another_black_row.py¶

Move Bit past the black squares.

No description has been provided for this image No description has been provided for this image

NOTES

Point out how can_move_right() returns False, but not turns it into True.

Helpful Bit Fact #3¶

Bit can tell you what color it is sitting on.

bit.is_on_blue()
bit.is_on_green()
bit.is_on_red()
bit.is_on_white()

green_path.py¶

Move Bit to the first clear square after the green squares.

No description has been provided for this image No description has been provided for this image

Move Bit to the blue square.

No description has been provided for this image No description has been provided for this image

blue_dot.py¶

🖌 while True¶

What happens when the condition of a while loop is always True?

🤔

infinite_loop.py¶

NOTES

Bit has an "infinite loop detection" feature, so if you make a mistake and create an infinite loop, it won't be too much of a hassle.

Getting stuck in an infinite loop is not problem (your computer will not explode), only an inconvenience. Even experienced programmers still run into them from time to time.

Also, not all infinite loops are mistakes. Sometimes you actually do want a loop to run forever (or close to it).

Now that you have these colors and tools at your command, let's paint something.

👩🏾‍🎨 👨🏼‍🎨

👨🏼‍🎨 Fix the Tree¶

fix_tree.py¶

Bit has a nice tree in his yard, but it is missing its trunk!

Help Bit fix the tree by adding the trunk.

No description has been provided for this image
No description has been provided for this image

When solving complex problems:¶

  • Break the problem into smaller pieces
    • What is the first goal? What is the second goal?
    • Solve the first piece before you start the second piece. Make sure the first piece is working correctly.
    • Do NOT try to solve the whole problem all at the same time.
  • Don't try to do it all in your head!
    • DRAW PICTURES

NOTES

Make sure to draw this out. Demonstrate how the students can use drawing to grasp the nature of the problem and design a strategy.

Demonstrate how drawing can be used to clarify boundary conditions.

Give the students time to discuss together (in pairs) how they would break the problem up.

Demonstrate the process of going from abstract stages to concrete, technical requirements. For example, getting to the red square is an abstract stage. Deciding that you need a while loop with an looping condition of not bit.is_on_red() and a body with bit.move() but no painting are the technical requirements.

Where will the state be after the first stage completes? Where will Bit be? What direction will he be facing?

Goal #0¶

Setup the file.

Map out the general strategy. Use code comments to capture your thoughts.

fix_tree.py¶

Goal #1¶

Get Bit to the red square

NOTES

Write functions for the high-level goals: get to trunk, fix the trunk

What is the condition? bit.is_on_red()? bit.is_on_white()? not bit.is_on_red()?

What if there were little green bushes in the way? Would bit.is_on_white() still work?

What if the base of the tree were blue? Would not bit.is_on_red() still work?

There are many ways to solve a problem. If the nature of the problem changes, even a little, you will likely need to adapt your code (e.g. change bit.is_on_white() to not bit.is_on_red()). This is a natural, essential part of the programming experience.

Goal #2¶

Fill in the trunk

NOTES

What is the loop condition?

Try first:

bit.move()
bit.paint('red')

Use the stepper to back off the error and observe where the problem occurred.

Observe how the paint('red') clobbers the square and colludes with the loop condition.

Note how the loop body and condition interact. Planning out and thinking through this interaction is important.

With move-then-paint, do you move first to get off the original red square, or do you simply repaint it?

Sometimes it is more convenient to repaint a square or two. That's fine. But avoid recless repainting (i.e. inserting paint statements all over your code) as a workaround to poorly-understood boundary conditions.

Use the snapshot feature to show how the students can coordinate stages of their solution with positions in their code.

Finally, show how the code works on both problems (switch between the two tabs).

🧑🏼‍🎨 Blue Ocean¶

blue_ocean.py¶

Fill the world with blue.

No description has been provided for this image
No description has been provided for this image

Bit gives us move, paint, etc.

What new verbs would make this job even easier?

NOTES

We've done problems like this before (nested while, etc.), but this is the first time we are using functions to define the pieces. The emphasis of this exercise is on the decomposition and abstraction of the parts, and then putting those parts together.

Several strategies to consider:

  • Row-by-row, zig-zag
  • Row-by-row, down-and-back
  • Column-by-column
  • Spiral

Which seems easiest? Why?

  • Computers (and humans!) don't do well will uncertainty. We like strategies with 0% uncertainty.
  • When we loop through a block of code, we want to know exactly where Bit will be before and after each iteration.
  • When we call a function, we want to know exactly where Bit will be before and after the function call.

Activity

  • implement go using fill_row_with_blue(bit) (even though it isn't defined yet)

  • Then define and implement fill_row_with_blue

  • What are the boundary conditions of fill_row_with_blue?

    • Where does bit start?
    • Where does bit end?
    • Notice how the prep (turn up) and the down-and-back make the problem easier

Remember¶

  • Choose strategies that minimize uncertainty
  • Pick concise but accurate and helpful names for your functions
  • Document the purpose and boundary conditions of the function
    • What does the function accomplish?
    • Where does Bit start? What direction is he facing?
    • Where does Bit end? What direction is he facing?

Key ideas¶

  • Identifying technical requirements by comparing inputs to a problem
  • Practice decomposing complex problems

Key Ideas¶

  • while
  • not
  • Boundary conditions
  • Infinite loops

New Bit methods

bit.can_move_front()
bit.can_move_left()
bit.can_move_right()
bit.is_on_blue()
bit.is_on_green()
bit.is_on_red()
bit.is_on_white()